/**
 * 
 */
package org.kopsox.comm.listener.impl;

import java.util.Collection;
import java.util.TreeSet;

import org.kopsox.comm.listener.Observed;
import org.kopsox.comm.listener.Observer;
import org.kopsox.comm.message.ValueMessage;

/**
 * Abstract implementation of the Observed-Interface.<br>
 * <b>NOTE:</b> This implementation is NOT thread safe, if you call the DefaultConstructor from your subclass!<br>
 * 
 * @author Konrad Renner - 22.10.2010
 *
 */
public abstract class AbstractObserved implements Observed {
	
	private final Collection<Observer> observer;
	
	public AbstractObserved() {
		this.observer = new TreeSet<Observer>();
	}
	
	public AbstractObserved(final Collection<Observer> implementation) {
		if(implementation == null) {
			throw new IllegalArgumentException("Null is not allowed");
		}
		
		this.observer = implementation;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#addAllObservers(java.util.Collection)
	 */
	@Override
	public boolean addAllObservers(final Collection<Observer> argobserver) {
		if(argobserver==null || argobserver.size() == 0) {
			return false;
		}
		return this.observer.addAll(argobserver);
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#addObserver(org.kopsox.comm.listener.Observer)
	 */
	@Override
	public boolean addObserver(final Observer argobserver) {
		if(argobserver==null) {
			return false;
		}
		
		return this.observer.add(argobserver);
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#getAllObservers()
	 */
	@Override
	public Collection<Observer> getAllObservers() {
		return this.observer;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#notifyObservers(org.kopsox.comm.message.ValueMessage)
	 */
	@Override
	public void notifyObservers(final ValueMessage message) {
		for(Observer obs: this.observer) {
			obs.notification(message);
		}
	}

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.Observed#removeObserver(org.kopsox.comm.listener.Observer)
	 */
	@Override
	public boolean removeObserver(final Observer argobserver) {
		return this.observer.remove(argobserver);
	}

}
